<?php

namespace LaraPress\Config;

use ReduxFramework;

class ReduxBuilder
{
    /**
     * Redux 实例
     *
     * @var ReduxFramework
     */
    public $redux;

    /**
     * 配置参数
     *
     * @var array
     */
    private $config;

    /**
     * 字段列表
     *
     * @var array
     */
    private $sections;

    /**
     * 构造函数
     *
     * @param array $config 基本配置
     * @param array $sections 字段配置
     */
    public function __construct($config, $sections = [])
    {
        if (!class_exists('ReduxFramework')) {
            return;
        }

        $this->config = $this->mergeDefaultConfig($config);
        $this->sections = $sections;
        $this->init();
    }

    /**
     * 初始化 Redux
     */
    public function init()
    {
        $this->redux = new ReduxFramework($this->sections, $this->config);
    }

    /**
     * 合并默认配置
     *
     * @param array $config 用户配置
     * @return array 合并后的配置
     */
    private function mergeDefaultConfig($config)
    {
        $defaults = [
            'opt_name'                  => 'wp_lara_press_options',
            'display_name'              => 'Larapress 设置',
            'display_version'           => defined('WP_LARA_PRESS_VERSION') ? WP_LARA_PRESS_VERSION : '1.0.0',
            'menu_type'                 => 'menu',
            'allow_sub_menu'            => true,
            'menu_title'                => 'Larapress 设置',
            'page_title'                => 'Larapress 设置',
            'async_typography'          => true,
            'admin_bar'                 => false,
            'admin_bar_icon'            => 'dashicons-portfolio',
            'admin_bar_priority'        => 50,
            'global_variable'           => 'wp_lara_press_options',
            'dev_mode'                  => false,
            'update_notice'             => true,
            'customizer'                => false,
            'page_priority'             => null,
            'page_parent'               => '',
            'page_permissions'          => 'manage_options',
            'menu_icon'                 => 'dashicons-admin-generic',
            'last_tab'                  => '',
            'page_icon'                 => 'icon-themes',
            'page_slug'                 => 'wp-lara-press-options',
            'save_defaults'             => true,
            'default_show'              => false,
            'default_mark'              => '*',
            'show_import_export'        => true,
            'transient_time'            => 60 * MINUTE_IN_SECONDS,
            'output'                    => true,
            'output_tag'                => true,
            'database'                  => '',
            'use_cdn'                   => true,
            'language'                  => 'zh_CN',
            'hints'                     => [
                'icon'          => 'el el-question-sign',
                'icon_position' => 'right',
                'icon_color'    => 'lightgray',
                'icon_size'     => 'normal',
                'tip_style'     => [
                    'color'   => 'light',
                    'shadow'  => true,
                    'rounded' => false,
                    'style'   => '',
                ],
                'tip_position'  => [
                    'my' => 'top left',
                    'at' => 'bottom right',
                ],
                'tip_effect'    => [
                    'show' => [
                        'effect'   => 'slide',
                        'duration' => '500',
                        'event'    => 'mouseover',
                    ],
                    'hide' => [
                        'effect'   => 'slide',
                        'duration' => '500',
                        'event'    => 'click mouseleave',
                    ],
                ],
            ],
            'show_options_object'       => false,
        ];

        return array_merge($defaults, $config);
    }

    /**
     * 获取配置值
     *
     * @param string $key 配置键
     * @param mixed $default 默认值
     * @return mixed
     */
    public function getOption($key, $default = null)
    {
        if (!$this->redux) {
            return $default;
        }

        $options = get_option($this->config['opt_name'], []);
        return isset($options[$key]) ? $options[$key] : $default;
    }

    /**
     * 设置配置值
     *
     * @param string $key 配置键
     * @param mixed $value 配置值
     * @return bool
     */
    public function setOption($key, $value)
    {
        if (!$this->redux) {
            return false;
        }

        $options = get_option($this->config['opt_name'], []);
        $options[$key] = $value;

        return update_option($this->config['opt_name'], $options);
    }
}
